package com.ryskoo.customer.customer.repository;

import com.ryskoo.common.constant.CacheConstant;
import com.ryskoo.customer.customer.domain.CustomerPO;
import com.ryskoo.framework.core.bean.QueryMessageDTO;
import com.ryskoo.framework.core.repository.BasicRedisImpl;
import com.ryskoo.framework.core.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

/**
 * @author xman
 */
@Primary
@Repository
public class CustomerRedisImpl extends BasicRedisImpl<CustomerPO> implements ICustomerRepository {

    @Autowired
    private RedisUtil redisClient;

    @Autowired
    private ICustomerRepository customerRepository;

    @PostConstruct
    public void init() {
        this.setRedisClient(redisClient);
        this.setDao(customerRepository);
    }

    public CustomerRedisImpl() {
        super(CacheConstant.CUSTOMER_CUSTOMER_KEY);
    }

    /**
     * 商户后台 - 冻结/激活 会员
     *
     * @param idList
     * @param activeState
     * @return
     */
    @Override
    public void handleFreeze(List<String> idList, String activeState) {
        customerRepository.handleFreeze(idList, activeState);
        if (idList != null && idList.size() > 0) {
            for (String uuid : idList) {
                redisClient.remove(CacheConstant.CUSTOMER_CUSTOMER_KEY + uuid);
            }
        }
    }

    /**
     * 查询会员的用户名/手机号/编号 是否已存在
     *
     * @param loginName
     * @return
     */
    @Override
    public boolean checkLoginNameExist(String loginName, String entityNo, String uuid) {
        return customerRepository.checkLoginNameExist(loginName, entityNo, uuid);
    }

    /**
     * 商户后台 - 查询会员列表（连表查询）
     */
    @Override
    public List<CustomerPO> queryCustomerList(QueryMessageDTO qm, boolean byPage) {
        return customerRepository.queryCustomerList(qm, byPage);
    }

    /**
     * 商户后台 - 查询会员数量（连表查询）
     */
    @Override
    public int queryCustomerCount(QueryMessageDTO qm) {
        return customerRepository.queryCustomerCount(qm);
    }

    /**
     * 根据商户id查询商户的会员
     *
     * @param queryMessage 统一查询条件 {@link QueryMessageDTO}
     * @param byPage   是否分页
     * @return 信息列表 {@link List}&lt;{@link Map}&lt;{@link String}, {@link Object}&gt&gt;
     */
    @Override
    public List<Map<String, Object>> queryCustomerByVendorId(QueryMessageDTO queryMessage, boolean byPage) {
        return customerRepository.queryCustomerByVendorId(queryMessage, byPage);
    }

    /**
     * 根据商户id查询商户的会员的总数
     *
     * @param queryMessage 统一查询条件 {@link QueryMessageDTO}
     * @return 商户的会员总数
     */
    @Override
    public int getCustomerCountByVendorId(QueryMessageDTO queryMessage) {
        return customerRepository.getCustomerCountByVendorId(queryMessage);
    }

    /**
     * 根据时间统计商户的会员数
     * @param date
     * @param vendorUuid
     * @return
     */
	@Override
	public int getCustomerCountByDate(String date, String vendorUuid) {
		return customerRepository.getCustomerCountByDate(date, vendorUuid);
	}

}
